package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.annotation.UserContext;
import com.ruoyi.system.domain.vo.UserData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TbCustomerOrder;
import com.ruoyi.system.mapper.OfflinePurchaseMapper;
import com.ruoyi.system.domain.OfflinePurchase;
import com.ruoyi.system.service.IOfflinePurchaseService;

/**
 * 商品信息 ：与店铺关联Service业务层处理
 * 
 * @author wh
 * @date 2024-09-18
 */
@Service
public class OfflinePurchaseServiceImpl implements IOfflinePurchaseService 
{
    @Autowired
    private OfflinePurchaseMapper offlinePurchaseMapper;

    /**
     * 查询商品信息 ：与店铺关联
     * 
     * @param productId 商品信息 ：与店铺关联主键
     * @return 商品信息 ：与店铺关联
     */
    @Override
    public OfflinePurchase selectOfflinePurchaseByProductId(Long productId)
    {
        return offlinePurchaseMapper.selectOfflinePurchaseByProductId(productId);
    }

    /**
     * 查询商品信息
     * 
     * @param offlinePurchase 商品信息 ：与店铺关联
     * @return 商品信息 ：与店铺关联
     */
    @Override
    public List<OfflinePurchase> selectOfflinePurchaseList(OfflinePurchase offlinePurchase)
    {
        // 使用 userData
        try {
            UserData userData = UserContext.getUserData();

            int merchantId = userData.getMerchantId();

            int storeId = userData.getStoreId();

            offlinePurchase.setMerchantId((long) merchantId);

            offlinePurchase.setStoreId((long) storeId);

            return offlinePurchaseMapper.selectOfflinePurchaseList(offlinePurchase);
        }finally {
            // 清理上下文，确保即使发生异常也能执行清理
            UserContext.clear();
        }





    }


    /**
     * 新增订单管理信息
     * 
     * @param offlinePurchase 商品信息 ：与店铺关联对象
     */
    public void insertTbCustomerOrder(OfflinePurchase offlinePurchase)
    {
        List<TbCustomerOrder> tbCustomerOrderList = offlinePurchase.getTbCustomerOrderList();
        Long productId = offlinePurchase.getProductId();
        if (StringUtils.isNotNull(tbCustomerOrderList))
        {
            List<TbCustomerOrder> list = new ArrayList<TbCustomerOrder>();
            for (TbCustomerOrder tbCustomerOrder : tbCustomerOrderList)
            {
                tbCustomerOrder.setMerchantId(productId);
                list.add(tbCustomerOrder);
            }
            if (list.size() > 0)
            {
                offlinePurchaseMapper.batchTbCustomerOrder(list);
            }
        }
    }
}
